
#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "ClientVersionDlg.h"

#define GET_TEXT(id, var) \
	GetDlgItemText(id, buf, 1024); \
	var = buf;

#define ATTACH(id, var) var.Attach(GetDlgItem(id))

LRESULT ClientVersionDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	
	getProfile();

	ATTACH(IDC_CLIENT_NAME, ctrlClientName);
	ATTACH(IDC_LAST_VERSION, ctrlLastVersion);
	ATTACH(IDC_CURRENT_VERSION, ctrlCurrentVersion);
	ATTACH(IDC_COMMENT, ctrlComment);
	ATTACH(IDC_ADDRESS, ctrlAddress);
	ATTACH(IDC_CLIENT_ADDRESS, ctrlClientAddress);
	ATTACH(IDC_ALERT, ctrlAlert);
	ATTACH(IDC_UPDATE_TIME, ctrlUpdateTime);
	ATTACH(IDC_UPDATED, ctrlUpdated);

	updateControls();
	
	CenterWindow(GetParent());
	return FALSE;
}

void ClientVersionDlg::getProfile() {
	ClientVersionManager::getInstance()->getClientVersion(currentVersionId, currentVersionProfile);

	clientName = currentVersionProfile.getClientName();
	lastVersion = currentVersionProfile.getLastVersion();
	currentVersion = currentVersionProfile.getCurrentVersion();
	comment = currentVersionProfile.getComment();
	address = currentVersionProfile.getAddress();
	clientAddress = currentVersionProfile.getClientAddress();
	updateTime = currentVersionProfile.getUpdateTime();
	alert = Util::toBool(currentVersionProfile.getAlert());
	updated = Util::toBool(currentVersionProfile.getUpdated());
}

void ClientVersionDlg::updateVars() {
	TCHAR buf[1024];

	GET_TEXT(IDC_CLIENT_NAME, clientName);
	GET_TEXT(IDC_LAST_VERSION, lastVersion);
	GET_TEXT(IDC_CURRENT_VERSION, currentVersion);
	GET_TEXT(IDC_COMMENT, comment);
	GET_TEXT(IDC_ADDRESS, address);
	GET_TEXT(IDC_CLIENT_ADDRESS, clientAddress);
	alert = ctrlAlert.GetCheck() == BST_CHECKED;
	GET_TEXT(IDC_UPDATE_TIME, updateTime);
	updated = ctrlUpdated.GetCheck() == BST_CHECKED;
}

void ClientVersionDlg::updateControls() {

	ctrlClientName.SetWindowText(clientName.c_str());
	ctrlLastVersion.SetWindowText(lastVersion.c_str());
	ctrlCurrentVersion.SetWindowText(currentVersion.c_str());
	ctrlComment.SetWindowText(comment.c_str());
	ctrlAddress.SetWindowText(address.c_str());
	ctrlClientAddress.SetWindowText(clientAddress.c_str());
	ctrlUpdateTime.SetWindowText(updateTime.c_str());
	ctrlAlert.SetCheck(alert ? BST_CHECKED : BST_UNCHECKED);
	ctrlUpdated.SetCheck(updated ? BST_CHECKED : BST_UNCHECKED);
	ctrlClientName.SetFocus();

}
LRESULT ClientVersionDlg::OnLink(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/){
	switch(wID){
		case IDC_SITE:	WinUtil::openLink(clientAddress);	break;
		case IDC_XML:	WinUtil::openLink(address);			break;
	}
	return 0;
}